<template>
  <div id="app">
    <TopBar :key="topbarKey" />
    <!-- 导航 -->
    <Header />
    <router-view @updataTopBarKey="updateKeyFn" />
    <!-- 底部 -->
    <Footer />
    <!-- 登录模态框 -->
    <!-- 需要一个状态控制login是否显示 该状态多个组件都需要使用到 因此存储在vuex中 -->
    <Login v-show="isShowLoginModel" />
    <transition name="slide">
      <Toast v-show="isShowToast" />
    </transition>
  </div>
</template>
<script>
import Header from "@/components/Header.vue";
import TopBar from "@/components/TopBar.vue";
import Footer from "@/components/Footer.vue";
import Toast from "@/components/Toast.vue";
import { JingpinAPI, UserProfilesAPI } from "@/request/api";
import Login from "@/components/Login.vue";
import { mapState } from "vuex";

export default {
  components: {
    Header,
    Footer,
    TopBar,
    Login,
    Toast,
  },
  data() {
    return {
      topbarKey: 0,
    };
  },
  methods: {
    updateKeyFn() {
      this.topbarKey++;
    },
  },
  computed: {
    ...mapState({
      isShowLoginModel: (state) => state.LoginModel.isShowLoginModel,
      isShowToast: (state) => state.showToast.isShowToast,
    }),
  },
  async created() {
    //缺点:
    //1.服务器url地址都一样,每个组件调用时豆豆需要复制过去  ----request
    //   一旦接口地址发生改变,所有调用了接口的组件都需要修改,---统一管理api.js
    //2.每次请求时都需要手动编写config携带token (多个组件携带代码重复)
    //3.优化then()写法.async+await

    //  JingpinAPI().then((res)=>{
    //    console.log('res22',res);//在network中看 请求地址,响应结果
    //  },(err)=>{
    //    console.log(err);
    //  })

    //优化方案
    let res = await JingpinAPI();
    console.log("优化方案res", res);
  },
  watch: {
    "$route.path": {
      handler(newVal, oldVal) {
        if (newVal != oldVal) {
          this.topbarKey++;
        }
      },
    },
  },
};
</script>

<style lang="less">
/* 入场的起始状态 = 离场的结束状态 */
.slide-enter,
.slide-leave-to {
  opacity: 0;
}

.slide-enter-active,
.slide-leave-active {
  transition: opacity 0.3s linear;
}

.slide-enter-to,
.slide-leave {
  opacity: 1;
}
</style>
